﻿using WeighClient.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;


namespace WeighClient.Utils
{
    public static class PrintUtils
    {
        public static void printWeighOrder(Bill billModel)
        {
            doPrint("tplWeighOrder", billModel);
        }
        private static void doPrint(string tplName,Bill billModel)
        {
            Excel.Application ExcelApp = new Excel.Application();
            ExcelApp.Visible = false;//是打开可见
            Excel.Workbooks wbks = ExcelApp.Workbooks;
            Excel._Workbook wbk = wbks.Add(System.AppDomain.CurrentDomain.BaseDirectory + "/Report/"+ tplName + ".xlsx");
            Excel.Worksheet ws = (Excel.Worksheet)wbk.Worksheets[1];

            billModel.CurTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            try
            {
                int rows = ws.UsedRange.Rows.Count;
                int columns = ws.UsedRange.Columns.Count;

                for (int i = 1; i <= rows; i++)
                {
                    for (int j = 1; j <= columns; j++)
                    {
                        if (null != ws.Cells[i, j] && null != ws.Cells[i, j].Value2)
                        {
                            string _val = ws.Cells[i, j].Value2.ToString();
                            if (!_val.StartsWith("${"))
                            {
                                continue;
                            }
                            string fieldName = _val.Substring(2, _val.Length - 3);
                            try
                            {
                                FieldInfo filedInfo = billModel.GetType().GetField(fieldName);
                                if (null != filedInfo.GetValue(billModel))
                                {
                                    ws.Cells[i, j] = filedInfo.GetValue(billModel).ToString();
                                }
                            }
                            catch { }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            wbk.PrintOutEx();

            KillProcess(ExcelApp); //杀掉生成的进程
            GC.Collect(); //垃圾回收机制
        }
        [DllImport("User32.dll")]
        public static extern int GetWindowThreadProcessId(IntPtr Hwnd, out int PID);
        private static void KillProcess(Excel.Application AppObject)
        {
            int Pid = 0;
            IntPtr Hwnd = new IntPtr(AppObject.Hwnd);
            System.Diagnostics.Process p = null;
            try
            {
                GetWindowThreadProcessId(Hwnd, out Pid);
                p = System.Diagnostics.Process.GetProcessById(Pid);
                if (p != null)
                {
                    p.Kill();
                    p.Dispose();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("进程关闭失败！异常信息：" + ex);
            }
        }
        
    }
}
